<template>
  <div class="goods-container">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" ref="ball" v-show="ballFlage"></div>
    </transition>
    <!--轮播-->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :bannerList="bannerList" :isfull="false"></swiper>
        </div>
      </div>
    </div>

    <!--商品购买-->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsInfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            <span>
              市场价格：
              <b class="old">￥{{goodsInfo.oldprice}}</b>
            </span>
            <span>
              销售价：
              <b class="now">￥{{goodsInfo.price}}</b>
            </span>
          </p>
          <p class="numbox" ref="numbox">
            购买数量：
            <numbox @getcount="getSelectedCount" :max="goodsInfo.stock"></numbox>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>
    <!--商品参数-->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsInfo.itemNumber}}</p>
          <p>库存情况：{{goodsInfo.stock}}</p>
          <p>上架时间：{{goodsInfo.addTime | dateFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <p>
          <mt-button type="primary" size="large" plain @click="goDesc(goodsInfo.id)">图文介绍</mt-button>
        </p>
        <p>
          <mt-button type="danger" size="large" plain>商品评论</mt-button>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
//导入轮播图组件
import swiper from "../subcomponents/swiper.vue";
//导入数量组件
import numbox from "../subcomponents/goodsinfo_numbox.vue";
export default {
  data() {
    return {
      goodsInfo: {},
      bannerList: [],
      ballFlage: false, //控件小球显示隐藏状态
      selectedCount: 1
    };
  },
  created() {
    this.getBanner();
    this.getGoodsInfo();
  },
  methods: {
    getGoodsInfo() {
      this.$http.get("goodsInfo#!method=get").then(result => {
        if (result.status === 200) {
          this.goodsInfo = result.body.message[0];
        } else {
          Toast("商品详情获取失败！");
        }
      });
    },
    getBanner() {
      //获取轮播图数据的方法
      this.$http.get("banner#!method=get").then(result => {
        // console.log(result);
        if (result.status === 200) {
          //当使用组件时，可能参数名称不一样，比如  首页 banner 图片字段是img 而产品详情的图片字段是 url
          result.body.bannerList.forEach(item => {
            item.img = item.url;
          });
          this.bannerList = result.body.bannerList;
        } else {
          Toast("加载轮播图失败");
        }
      });
    },
    goDesc(id) {
      this.$router.push({ name: "goodsDesc", params: { id } });
    },
    addToShopCar() {
      //添加到购物车
      this.ballFlage = !this.ballFlage;

      //拼接出一个要保存到 store 中 car 数组里的商品信息对象
      var goodsCarInfo = {
        id: this.goodsInfo.id,
        count: this.selectedCount,
        price: this.goodsInfo.price,
        selected: true
      };

      //调用 store 中的 mutations 中的方法来存储数据
      this.$store.commit('addToCar',goodsCarInfo)
      
    },
    beforeEnter(el) {
      //获取按钮的坐标
      const numboxPosition2 = this.$refs.numbox.getBoundingClientRect();
      el.style.transform = `translate(${numboxPosition2.left +
        124}px, ${numboxPosition2.top + 6}px)`;
    },
    enter(el, done) {
      el.offsetWidth;

      //小球动画优化思路
      //原因：位置不能写死。
      //只要分辨率不同 或者 滚动 条变化， 那么问题就会出现。
      //因此这个位移的横众坐标不能写死，只能通过动态去计算。
      //用什么技术实现 拿到两个点的 坐标信息 两坐标求差
      //domObject.getBoundingClientRect().top  domObject.getBoundingClientRect().left  ..

      //获取购物车的坐标
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      el.style.transform = `translate(${badgePosition.left}px, ${
        badgePosition.top
      }px)`;
      el.style.transition = "all 0.5s cubic-bezier(.09,-0.19,1,.54)";

      done();
    },
    afterEnter(el) {
      this.ballFlage = !this.ballFlage;
    },
    getSelectedCount(count) {
      this.selectedCount = count;
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>

<style lang="scss" scoped>
.goods-container {
  background: #eee;
  min-height: 100%;
  overflow: hidden;
  .mui-card-footer {
    display: block;
    p {
      padding: 5px 0;
    }
  }
  .mui-card-content-inner {
    p {
      padding: 5px 0;
    }
  }
  .price {
    .now {
      font-size: 12px;
      color: #ff0b04;
      font-weight: normal;
    }
    .old {
      font-size: 12px;
      color: #999;
      text-decoration: line-through;
      font-weight: normal;
      padding-right: 10px;
    }
  }
}
.ball {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: #ff0000;
  position: fixed;
  z-index: 9999999;
  top: 0;
  left: 0;
}
</style>
